using System;
using System.Data.Services.Client;

namespace OrbitOne.CRMLyncInfoDashboard.CRM.OrganizationData
{
    public class PagingContext<T>
    {
        public LabContext ServiceContext { get; private set; }
        public DataServiceQuery<T> Query { get;  set; }
        public DataServiceCollection<T> Results { get; set; }
        public Func<DataServiceCollection<T>, bool> PageProcessor
        {
            get
            {
                return results =>
                           {
                               try
                               {
                                   if (null == Results)
                                   {
                                       Results = new DataServiceCollection<T>(ServiceContext);
                                       Results.Load(results);
                                   }
                                   else
                                   {
                                       foreach (T t in results)
                                       {
                                           Results.Add(t);
                                       }
                                   }
                               }
                               catch (Exception ex)
                               {
                                   throw;
                               }

                               return true;
                           };
            }

        }

        public PagingContext(LabContext labContext,
                             DataServiceQuery<T> query)
        {
            ServiceContext = labContext;
            Query = query;
        }
    }
}